package com.itheima.controller;

import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.service.PermissionService;
import com.itheima.health.utils.resources.MessageConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//import org.springframework.security.access.prepost.PreAuthorize;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.core.userdetails.User;

@RestController   //  @Controller +@ResponseBody
@Api(tags = "检查项管理")
public class permissionController {

    @Reference
    private PermissionService permissionService;

    //   查询所有权限
    @GetMapping("permission/findAll")
    //  @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findAll(){
        try {

            List<Permission> list = permissionService.findAll();
            return  new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

     @PostMapping("permission/findPage")
   //  @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
           PageResult pageResult =  permissionService.findPage(queryPageBean);
            return  new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    @PostMapping("permission/add")
    public Result add(@RequestBody Permission permission){
        try {

            permissionService.saveOrUpdate(permission);
            if(StringUtils.isEmpty(permission.getId())){
                //  insert
                return  new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
            }else{
                // update
                return  new Result(true, MessageConstant.ADD_PERMISSION_FAIL);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,"更新失败！");
        }
    }
    @DeleteMapping("permission/deletePermissionById/{id}")
    public Result deleteCheckItemById(@PathVariable("id") int id) {
        try {
            Permission permission = new Permission();
            permission.setIs_delete(1);  //  逻辑删除
            permission.setId(id);
            permissionService.updateById(permission);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }


}
